﻿using System;
using System.Linq.Expressions;
using System.Reflection;

namespace Dapper.SimpleDao.Factory
{
    public static class PredicateHelper
    {
        /// <summary>
        /// 实体Lambda字段查询转换
        /// </summary>
        /// <typeparam name="T">数据实体</typeparam>
        /// <param name="expression">表达式</param>
        /// <param name="op">操作</param>
        /// <param name="value">字段的值</param>
        /// <param name="not">是否有等于</param>
        /// <returns></returns>
        public static FieldPredicate<T> Field<T>(Expression<Func<T, object>> expression, Operator op, object value, bool not = false) where T : class
        {
            PropertyInfo propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
            return new FieldPredicate<T>
            {
                PropertyName = propertyInfo.Name,
                Operator = op,
                Value = value,
                Not = not
            };
        }
        /// <summary>
        /// 转换操作符
        /// </summary>
        /// <param name="op">操作枚举</param>
        /// <param name="not">是否有等于</param>
        /// <returns></returns>
        public static string GetOperatorString(Operator op, bool not)
        {
            switch (op)
            {
                case Operator.Gt:
                    return not ? "<=" : ">";
                case Operator.Ge:
                    return not ? "<" : ">=";
                case Operator.Lt:
                    return not ? ">=" : "<";
                case Operator.Le:
                    return not ? ">" : "<=";
                case Operator.Like:
                    return not ? "NOT LIKE" : "LIKE";
                default:
                    return not ? "<>" : "=";
            }
        }
    }
}
